<?php

/**
 * PagerTest
 *
 * @author vbolshov
 */

use km\util\Validator;

use \PHPUnit_Framework_TestCase;

require_once dirname(__FILE__) . '/../../lib/Km.php';
require_once dirname(__FILE__) . '/../../lib/km/util/Validator.php';
require_once dirname(__FILE__) . '/../../lib/km/util/Validator/Result.php';

class ValidatorTest extends PHPUnit_Framework_TestCase {
	function testConditionalValidator()
	{
		$unconditional = new Validator();
		
		$conditional = new Validator();
		$conditional->addRequired('something', 'something went wrong');
		
		$test = function($data) {
			// Only undergo validation with $conditional validator
			// in case when 'condition' key is present in validated data
			return array_key_exists('condition', $data);
		};
		$unconditional->addConditional($conditional, $test);
		
		// a check with ENABLED conditional validator ('condition' key is present in validated data)
		$result = $unconditional->check(array('condition' => true));
		$this->assertFalse($result->ok());
		$errors = $result->getErrorList(true);
		$this->assertTrue(array_key_exists('something', $errors));
		$this->assertEquals('something went wrong', $errors['something']);
		
		// a check with DISABLED conditional validator ('condition' key is NOT present in validated data),
		// should always pass
		$this->assertTrue($unconditional->check(array())->ok());
	}
}